/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <flgrCoreMalloc.h>
#include <flgrCoreDispatch.h>
#include "flgrDataToolsStack.h"


/*!
 * \defgroup group_fulguro_datatools_stack Stack
 * \brief Stack
 * \ingroup group_fulguro_datatools
 * @{
 */


void flgr_stack_init(FLGR_Stack *stack) {
  stack->top = NULL;
  stack->next = NULL;
  stack->previous = NULL;
  stack->label = NULL;
  stack->size = 0;
}

int flgr_stack_push(FLGR_Stack *stack, int x, int y, int z) {
  FLGR_StackLink *tmp = flgr_malloc(sizeof(FLGR_Stack));

  tmp->lower = stack->top;
  tmp->x = x;
  tmp->y = y;
  tmp->z = z;

  stack->size++;

  stack->top = tmp;

  return stack->size;
}

int flgr_stack_pop(FLGR_Stack *stack, int *x, int *y, int *z) {
  FLGR_StackLink *tmp = stack->top;

  if(stack->size<=0) return 0;
  
  stack->size--;

  tmp = stack->top;
  stack->top = tmp->lower;

  *x = tmp->x;
  *y = tmp->y;
  *z = tmp->z;

  flgr_free(tmp);

  return stack->size;
}

FLGR_Stack* flgr_stack_get_next(FLGR_Stack *stack) {
  return stack->next;
}

FLGR_Stack* flgr_stack_get_previous(FLGR_Stack *stack) {
  return stack->previous;
}

int flgr_stack_get_size(FLGR_Stack *stack) {
  return stack->size;
}

void flgr_stack_flush(FLGR_Stack *stack) {
  int x,y,z;
  int v = flgr_stack_get_size(stack);

  while(v>0) {
    v = flgr_stack_pop(stack,&x,&y,&z);
  }
}



//! @}
